package com.work.shart.web.controller.goods;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.work.shart.common.mybatis.service.impl.SuperServiceImpl;
import com.work.shart.common.result.ResultVo;
import com.work.shart.web.entity.classifcation.ClassificationEntity;
import com.work.shart.web.entity.goods.GoodsEntity;
import com.work.shart.web.res.goods.GoodsRes;
import com.work.shart.web.service.classifcation.IClassificationService;
import com.work.shart.web.service.goods.GoodsEsService;
import com.work.shart.web.service.goods.IGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.work.shart.common.result.Result.success;

@RestController
@RequestMapping("/api/goods")
@Slf4j
@Api(tags = "摘要文章")
public class GoodsController extends SuperServiceImpl<IGoodsService, GoodsEntity> {

    @Autowired
    private GoodsEsService goodsEsService;

    @Autowired
    private IClassificationService classificationService;

    @ApiOperation(value = "文章列表(如果不传参数代表所有、传参数将全文检索)")
    @GetMapping("list")
    public ResultVo<List<GoodsRes>> list(
            @ApiParam(value = "搜索内容") @RequestParam(value = "content", required = false) String content,
            @ApiParam(value = "类别id") @RequestParam(value = "classifcationId", required = false) Long classifcationId
    ) {
        if (StrUtil.isEmpty(content)) {
            QueryWrapper<GoodsEntity> queryWrapper = new QueryWrapper<>();
            if (ObjectUtil.isNotNull(classifcationId))
                queryWrapper.eq("classifation_id", classifcationId);
            List<GoodsEntity> goodsEntities = getBaseService().list(queryWrapper);
            List<GoodsRes> goodsRes = Lists.newArrayList();
            goodsEntities.forEach(goodsEntity -> {
                ClassificationEntity classificationEntity = classificationService.getById(goodsEntity.getClassifationId());
                GoodsRes res = BeanUtil.copyProperties(goodsEntity, GoodsRes.class, "classification");
                if (classificationEntity != null)
                    res.setClassification(classificationEntity);
                goodsRes.add(res);
            });
            return success(goodsRes);
        }
        return success(goodsEsService.querySearch(content));
    }

    @ApiOperation(value = "热门推荐文章")
    @GetMapping("popularRecommendation")
    public  ResultVo<GoodsEntity> popularRecommendation(){
        QueryWrapper<GoodsEntity>goodsEntityQueryWrapper=new QueryWrapper<>();
        goodsEntityQueryWrapper.gt("reading_volume",50);
        return success( getBaseService().list(goodsEntityQueryWrapper));
    }


    @ApiOperation(value = "文章索引，可根据内容检索的列表")
    @GetMapping("all")
    public ResultVo geta() {
        return success(goodsEsService.all());
    }

}
